class Queue {
  constructor() {
    this.items = []
  }

  enqueue(element){
    this.items.push(element)
  }

  dequeue(){
    this.items.shift()
  }

  front(){
    if (this.items.length === 0) return null;
    return this.items[0]
  }

  isEmpty() {
    return this.items.length === 0;
  }

  size() {
    return this.items.length
  }
}

function passGame(nameList, num){
  // 1.创建队列
  const queue = new Queue();
  for (const name of nameList) {
    queue.enqueue(name);
  }
  // 2.循环进入队列
  while(queue.size() > 1) {
    // 比如说4个人，从0开始数，数到2出去
    // 既然数到2就要出去，i就不需要遍历到2
    for (let i = 0; i < num; i++) {
      queue.enqueue(queue.front());
      queue.dequeue()
    }
    queue.dequeue();
  }
  return queue.front();
}

const nameList = ["lily", "lucy", "tom", "tony", "jack"];
console.log(passGame(nameList, 2));
// tony jack lily lucy
// lucy tony jack
// lucy tony
// tony